<template>
	<div >
		<el-card shadow="hover">
			<div class=" mb15">
				<el-form :model="tableData.param" ref="queryRef" :inline="true" label-width="100px">
					<el-row>
						<el-col :span="6" class="colBlock">
							<el-form-item label="api名称" prop="name">
								<el-input v-model="tableData.param.name" placeholder="请输入api名称" clearable
									@keyup.enter.native="getList" />
							</el-form-item>
						</el-col>
						<el-col :span="6" class="colBlock">
							<el-form-item label="分组" prop="groupCode">
								<el-select v-model="tableData.param.groupCode" placeholder="请选择设备分组" clearable>
									<el-option v-for="item in configGroupData" :key="item.code" :label="item.name"
										:value="item.code" />
								</el-select>
							</el-form-item>
						</el-col>
						<el-col :span="6" class="colBlock">
							<el-form-item label="分组状态" prop="status">
								<el-select v-model="tableData.param.status" placeholder="请选择设备分组" clearable>
									<el-option label="开放" :value="true" />
									<el-option label="不开放" :value="false" />
								</el-select>
							</el-form-item>
						</el-col>
						<el-col :span="6" class="colBlock">
							<el-form-item>
								<el-button type="primary" @click="getList"><el-icon>
										<Search />
									</el-icon>搜索</el-button>
								<el-button @click="resetQuery(queryRef)"><el-icon>
										<Refresh />
									</el-icon>重置</el-button>
								<el-button type="primary" @click="handleAdd" v-if="permissionStore.isPermissionsBtn('003002001')"><el-icon>
										<Plus />
									</el-icon>新增</el-button>
							</el-form-item>
						</el-col>
					</el-row>
				</el-form>
			</div>
			<el-table v-loading="loading" :data="tableData.data">
				<el-table-column label="配置名称" align="center" prop="name"  min-width="100px" />
				<el-table-column label="配置状态" align="center" prop="name" min-width="100px">
					<template #default="scope">
						<el-switch v-model="scope.row.status" class="mb-2" :disabled="!permissionStore.isPermissionsBtn('003002004')" @change="handleStatusChange(scope.row)" />
					</template>
				</el-table-column>
				<el-table-column label="分组" align="center" prop="name" min-width="100px" >
					<template #default="{ row }">
						<el-tag v-for="tag in row.group" :key="tag" class="tag-item ml5 " >
							{{ tag.name }}
						</el-tag>
					</template>
				</el-table-column>
				<el-table-column label="所属API" align="center" prop="name" min-width="100px" >
					<template #default="{ row }">{{ row.api.apiName }}</template>
				</el-table-column>
				<el-table-column label="权限" align="center" prop="name" min-width="100px" >
					<template #default="{ row }">
						<el-tag v-for="tag in row.auth" :key="tag" class="tag-item ml5" >
							{{ tag }}
						</el-tag>
					</template>
				</el-table-column>
				<el-table-column label="是否开放" align="center" prop="name" min-width="100px" >
					<template #default="{ row }">
						{{ row.sysStatus?'开放':'不开放' }}
					</template>
				</el-table-column>
				
				<el-table-column label="操作" align="center" class-name="small-padding" min-width="200px" fixed="right">
					<template #default="scope">
						<el-button type="primary" link @click="handleView(scope.row)" 
							><el-icon><View /></el-icon>详情</el-button
						>
						<el-button type="primary" link @click="handleUpdate(scope.row)"  v-if="permissionStore.isPermissionsBtn('003002002')"
							><el-icon><EditPen /></el-icon>修改</el-button
						>
						<el-button type="danger" link @click="handleDelete(scope.row)"  v-if="permissionStore.isPermissionsBtn('003002003')"
							><el-icon><DeleteFilled /></el-icon>删除</el-button
						>
					</template>
				</el-table-column>
			</el-table>
			
		</el-card>
		<addView ref="addRef" @getList="getList" :configUserTypeData="configUserTypeData" ></addView>
		<editView ref="editRef" @getList="getList" :configUserTypeData="configUserTypeData"></editView>
		<detailView ref="detailRef" @getList="getList"></detailView>
	</div>
</template>
<script lang="ts">
import { configUserListApi,configUserGroupApi,configUserTypeApi ,editConfigUserStatusApi,delConfigUserApi} from '@/api/thirdManage/apiAccount';
import { toRefs, reactive, onMounted, ref, defineComponent, computed, getCurrentInstance, toRaw } from 'vue';
import { ElMessageBox, ElMessage, FormInstance } from 'element-plus';
import { usePermissionStore } from "@/store/modules/permission"
import addView from './component/add.vue';
import editView from './component/edit.vue';
import detailView from './component/detail.vue';
export default defineComponent({
	name: 'apiAccountList',
	components: {
		addView,
		editView,
		detailView,
	},
	setup() {
		const { proxy } = <any>getCurrentInstance();
		const loading = ref(false);
		const queryRef = ref();
		const addRef = ref();
		const editRef = ref();
		const detailRef = ref();
		const permissionStore = usePermissionStore()
		// 是否显示所有搜索选项
		const showAll = ref(false);

		const word = computed(() => {
			if (showAll.value === false) {
				//对文字进行处理
				return '展开搜索';
			} else {
				return '收起搜索';
			}
		});
		const state = reactive({
			ids: [],
			tableData: {
				data: [],
				loading: false,
				param: {
					name:null,
					apiCode:null,
					groupCode:null,
					status:null
				},
			},
		});
		// 页面加载时
		onMounted(() => {
			initTableData();
			configGroup()
			configUserType()
		});
		// 初始化表格数据
		const initTableData = () => {
			getList();
		};
		/** 重置按钮操作 */
		const resetQuery = (formEl: FormInstance | undefined) => {
			if (!formEl) return;
			formEl.resetFields();
			getList();
		};
		// 获取列表数据
		const getList = () => {
			loading.value = true;
			if(!state.tableData.param.apiCode)state.tableData.param.apiCode=null
			if(!state.tableData.param.groupCode)state.tableData.param.groupCode=null
			if(!state.tableData.param.name)state.tableData.param.name=null
			if(!state.tableData.param.status)state.tableData.param.status=null
			configUserListApi(state.tableData.param).then((res: any) => {
				let list = res.data ?? [];
				state.tableData.data = list;
				loading.value = false;
			});
		};
		// 修改
		const handleUpdate = (row) => {
			editRef.value.openDialog(toRaw(row));
		};
		// 添加
		const handleAdd = () => {
			addRef.value.openDialog();
		};
		const handleView = (row) => {
			detailRef.value.openDialog(toRaw(row));
		};
		// 删除
		const handleDelete=(row)=>{
			let msg = `此操作将永久删除数据(${row.name})，是否继续?`;
			ElMessageBox.confirm(msg, '提示', {
				confirmButtonText: '确认',
				cancelButtonText: '取消',
				type: 'warning',
			})
				.then(() => {
					delConfigUserApi({id:row.id}).then(() => {
						ElMessage.success('删除成功');
						getList();
					});
				})
				.catch(() => {});
		}

		// 用户状态修改
		const handleStatusChange = (row: any) => {
			let text = row.status? "开启" : "关闭";
			ElMessageBox.confirm('确认要"' + text + '"："' + row.name + '"配置状态吗?', "警告", {
				confirmButtonText: "确定",
				cancelButtonText: "取消",
				type: "warning"
			}).then(function () {
				return editConfigUserStatusApi({id:row.id,status:row.status});
			}).then(() => {
				ElMessage.success(text + "成功");
			}).catch(function () {
				row.status = row.status? false : true;
			});
		};

		// 获取分组类型
		const configGroupData = ref([])
		const configGroup = () => {
			configUserGroupApi().then((res: any) => {
				if (res.data) configGroupData.value = res.data
			})
		}

		// 获取配置组类型
		const configUserTypeData = ref([])
		const configUserType = () => {
			configUserTypeApi().then((res: any) => {
				if (res.data) configUserTypeData.value = res.data
			})
		}

		return {
			proxy,
			addRef,
			editRef,
			detailRef,
			showAll,
			loading,
			word,
			queryRef,
			configGroupData,
			configUserTypeData,
			permissionStore,
			handleView,
			handleAdd,
			handleDelete,
			resetQuery,
			getList,
			handleUpdate,
			handleStatusChange,
			...toRefs(state),
		};
	},
});
</script>
<style lang="scss" scoped>
.colBlock {
	display: block;
}

.colNone {
	display: none;
}
</style>
